commonlibsse_ng\re\h/
hkReferencedObject.rs

1use core::sync::atomic::{AtomicI16, Ordering};
2
3use crate::re::hkBaseObject::hkBaseObject;
4use crate::re::offsets_rtti::RTTI_hkReferencedObject;
5use crate::re::offsets_vtable::VTABLE_hkReferencedObject;
6use crate::re::{hkClass, hkStatisticsCollector};
7use crate::rel::id::VariantID;
8
9/// Represents a reference-counted object in the Havok system.
10///
11/// Inherits from `hkBaseObject` and adds reference counting functionality.
12#[repr(C)]
13#[derive(Debug)]
14pub struct hkReferencedObject {
15    /// Base class `hkBaseObject`.
16    pub __base: hkBaseObject,
17
18    /// Combined memory size and flags.
19    /// - Offset: 0x08
20    pub memSizeAndFlags: u16,
21
22    /// Volatile mutable reference count.
23    /// - Offset: 0x0A
24    pub referenceCount: AtomicI16,
25
26    /// Padding to maintain memory alignment.
27    /// - Offset: 0x0C
28    pub pad0C: u32,
29}
30
31// Compile-time memory layout verification
32const _: () = {
33    assert!(core::mem::offset_of!(hkReferencedObject, __base) == 0x0);
34    assert!(core::mem::offset_of!(hkReferencedObject, memSizeAndFlags) == 0x08);
35    assert!(core::mem::offset_of!(hkReferencedObject, referenceCount) == 0x0A);
36    assert!(core::mem::offset_of!(hkReferencedObject, pad0C) == 0x0C);
37    assert!(core::mem::size_of::<hkReferencedObject>() == 0x10);
38};
39
40impl crate::re::hkRefPtr::hkRefPtrCounted for hkReferencedObject {}
41
42/// Lock mode enumeration for reference counting behavior.
43#[commonlibsse_ng_derive_internal::to_bitflags]
44#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
45#[repr(u32)]
46pub enum LockMode {
47    None = 0,
48    Auto = 1,
49    Manual = 2,
50}
51
52impl hkReferencedObject {
53    /// RTTI identifier for this type.
54    pub const RTTI: VariantID = RTTI_hkReferencedObject;
55
56    /// Virtual function table addresses.
57    pub const VTABLE: [VariantID; 1] = VTABLE_hkReferencedObject;
58
59    /// Maximum memory size constant.
60    pub const MEM_SIZE: u16 = 0x7FFF;
61
62    /// Creates a new `hkReferencedObject` with default values.
63    #[inline]
64    pub const fn new() -> Self {
65        Self {
66            __base: hkBaseObject::new(),
67            memSizeAndFlags: Self::MEM_SIZE,
68            referenceCount: AtomicI16::new(1), // Initial reference count of 1
69            pad0C: 0,
70        }
71    }
72
73    /// Adds a reference to this object by incrementing the reference count atomically.
74    #[commonlibsse_ng_derive_internal::relocate_fn(se_id = 56606, ae_id = 57010)]
75    #[inline]
76    pub fn add_reference(&self) {}
77
78    /// Removes a reference from this object by decrementing the reference count atomically.
79    #[commonlibsse_ng_derive_internal::relocate_fn(se_id = 56607, ae_id = 57011)]
80    #[inline]
81    pub fn remove_reference(&self) {}
82
83    /// Gets the current reference count.
84    #[inline]
85    pub fn get_reference_count(&self) -> i16 {
86        self.referenceCount.load(Ordering::Acquire)
87    }
88
89    /// Gets the allocated size (based on memSizeAndFlags).
90    #[inline]
91    pub const fn get_allocated_size(&self) -> i32 {
92        (self.memSizeAndFlags & Self::MEM_SIZE) as i32
93    }
94}
95
96/// Virtual function table for `hkReferencedObject`.
97#[repr(C)]
98pub struct hkReferencedObjectVtbl {
99    /// Destructor function pointer (hkBaseObject's virtual destructor is default).
100    pub CxxDrop: fn(this: &mut hkReferencedObject),
101
102    /// Gets the class type (returns null by default).
103    pub GetClassType: fn(this: &hkReferencedObject) -> Option<*const hkClass>,
104
105    /// Calculates content statistics.
106    pub CalcContentStatistics:
107        fn(this: &hkReferencedObject, collector: &mut hkStatisticsCollector, cls: Option<&hkClass>),
108}
109
110impl Default for hkReferencedObjectVtbl {
111    #[inline]
112    fn default() -> Self {
113        Self::new()
114    }
115}
116
117impl hkReferencedObjectVtbl {
118    /// Creates a new default virtual table with stubbed functions.
119    #[inline]
120    pub const fn new() -> Self {
121        const fn CxxDrop(_this: &mut hkReferencedObject) {}
122
123        const fn GetClassType(_this: &hkReferencedObject) -> Option<*const hkClass> {
124            None
125        }
126
127        const fn CalcContentStatistics(
128            _this: &hkReferencedObject,
129            _collector: &mut hkStatisticsCollector,
130            _cls: Option<&hkClass>,
131        ) {
132        }
133
134        Self { CxxDrop, GetClassType, CalcContentStatistics }
135    }
136}
137
138impl Default for hkReferencedObject {
139    #[inline]
140    fn default() -> Self {
141        Self::new()
142    }
143}